import { v4 as uuid } from 'uuid';
import update from './atom/db/update';
import query from './atom/db/query';

declare const action_id: string;
declare const session_id: string;
declare const spaceid: string;

interface Message {
	exam_id: string;
	exam_name: string;
	start_time: string;
	end_time: string;
	pm_start_time: string;
	am_start_time: string;
	creater: string;
}

async function atom(msg: Message) {
	const systemid = '5981';

	const query_obj = query(action_id, session_id, spaceid, systemid);
	query_obj.prepare('exam', ['_id', 'default_val'], { exam_id: msg.exam_id }, 1, 1);
	query_obj.prepare('exam', ['_id', 'default_val'], { default_val: '是' }, 1, 1);
	const [q_data, d_data] = await query_obj.exec();

	const { prepare, exec } = update(action_id, session_id, spaceid, systemid);

	const rows = [];
	const rowdata: any = q_data[0];
	rowdata.default_val = '是';
	rows.push(rowdata);

	if (d_data.length > 0) {
		const d_row: any = d_data[0];
		d_row.default_val = '否';
		rows.push(d_row);
	}

	prepare('exam', rows);
	const ret = await exec();
	if (ret.code === 1) {
		return 'success';
	} else {
		return 'fail';
	}
};
